package com.cloud.qualitycontrol.service.impl;

import com.cloud.qualitycontrol.dao.QualityTreeRepository;
import com.cloud.qualitycontrol.model.QualityNode;
import com.cloud.qualitycontrol.service.QualityTreeService;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Set;

import static com.cloud.qualitycontrol.util.CommonVar.*;


@Service
public class QualityTreeServiceImpl implements QualityTreeService {

    @Autowired
    private QualityTreeRepository qualityTreeRepository;

    @Override
    public QualityNode addNode(final QualityNode qualityNode) {
        return qualityTreeRepository.saveAndFlush(qualityNode);
    }

    @Override
    public String initTree() {
        QualityNode root = new QualityNode();
        root.setName("质量管理").setParentId("0").setRootId("0");
        root = qualityTreeRepository.saveAndFlush(root);

        QualityNode qualityNode_a = new QualityNode();
        qualityNode_a.setName("质量管理体系").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_a = qualityTreeRepository.saveAndFlush(qualityNode_a);

        QualityNode qualityNode_a_a = new QualityNode();
        qualityNode_a_a.setName("承包单位质量管制制度").setParentId(qualityNode_a.getId()).setRootId(root.getId());
        qualityNode_a_a = qualityTreeRepository.saveAndFlush(qualityNode_a_a);

        QualityNode qualityNode_b = new QualityNode();
        qualityNode_b.setName("工程质量责任人档案登记").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_b = qualityTreeRepository.saveAndFlush(qualityNode_b);

        QualityNode qualityNode_b_a = new QualityNode();
        qualityNode_b_a.setName("标段负责人").setParentId(qualityNode_b.getId()).setRootId(root.getId());
        qualityNode_b_a = qualityTreeRepository.saveAndFlush(qualityNode_b_a);

        QualityNode qualityNode_c = new QualityNode();
        qualityNode_c.setName("首件工程").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_c = qualityTreeRepository.saveAndFlush(qualityNode_c);

        QualityNode qualityNode_c_a = new QualityNode();
        qualityNode_c_a.setName("新增").setParentId(qualityNode_c.getId()).setRootId(root.getId());
        qualityNode_c_a = qualityTreeRepository.saveAndFlush(qualityNode_c_a);

        QualityNode qualityNode_c_b = new QualityNode();
        qualityNode_c_b.setName("审核").setParentId(qualityNode_c.getId()).setRootId(root.getId());
        qualityNode_c_b = qualityTreeRepository.saveAndFlush(qualityNode_c_b);

        QualityNode qualityNode_c_c = new QualityNode();
        qualityNode_c_c.setName("历史").setParentId(qualityNode_c.getId()).setRootId(root.getId());
        qualityNode_c_c = qualityTreeRepository.saveAndFlush(qualityNode_c_c);

        QualityNode qualityNode_d = new QualityNode();
        qualityNode_d.setName("专项施工方案").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_d = qualityTreeRepository.saveAndFlush(qualityNode_d);

        QualityNode qualityNode_d_a = new QualityNode();
        qualityNode_d_a.setName("新增").setParentId(qualityNode_d.getId()).setRootId(root.getId());
        qualityNode_d_a = qualityTreeRepository.saveAndFlush(qualityNode_d_a);

        QualityNode qualityNode_d_b = new QualityNode();
        qualityNode_d_b.setName("新增").setParentId(qualityNode_d.getId()).setRootId(root.getId());
        qualityNode_d_b = qualityTreeRepository.saveAndFlush(qualityNode_d_b);

        QualityNode qualityNode_e = new QualityNode();
        qualityNode_e.setName("质检资料").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_e = qualityTreeRepository.saveAndFlush(qualityNode_e);

        QualityNode qualityNode_e_a = new QualityNode();
        qualityNode_e_a.setName("施工单位").setParentId(qualityNode_e.getId()).setRootId(root.getId());
        qualityNode_e_a = qualityTreeRepository.saveAndFlush(qualityNode_e_a);

        QualityNode qualityNode_e_b = new QualityNode();
        qualityNode_e_b.setName("质检资料查看").setParentId(qualityNode_e.getId()).setRootId(root.getId());
        qualityNode_e_b = qualityTreeRepository.saveAndFlush(qualityNode_e_b);

        QualityNode qualityNode_e_a_a = new QualityNode();
        qualityNode_e_a_a.setName("新增").setParentId(qualityNode_e_a.getId()).setRootId(root.getId());
        qualityNode_e_a_a = qualityTreeRepository.saveAndFlush(qualityNode_e_a_a);

        QualityNode qualityNode_e_a_b = new QualityNode();
        qualityNode_e_a_b.setName("审核").setParentId(qualityNode_e_a.getId()).setRootId(root.getId());
        qualityNode_e_a_b = qualityTreeRepository.saveAndFlush(qualityNode_e_a_b);

        QualityNode qualityNode_f = new QualityNode();
        qualityNode_f.setName("关键工程部位联检").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_f = qualityTreeRepository.saveAndFlush(qualityNode_f);

        QualityNode qualityNode_f_a = new QualityNode();
        qualityNode_f_a.setName("新增").setParentId(qualityNode_f.getId()).setRootId(root.getId());
        qualityNode_f_a = qualityTreeRepository.saveAndFlush(qualityNode_f_a);

        QualityNode qualityNode_f_b = new QualityNode();
        qualityNode_f_b.setName("查看").setParentId(qualityNode_f.getId()).setRootId(root.getId());
        qualityNode_f_b = qualityTreeRepository.saveAndFlush(qualityNode_f_b);

        QualityNode qualityNode_g = new QualityNode();
        qualityNode_g.setName("监理指令").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_g = qualityTreeRepository.saveAndFlush(qualityNode_g);

        QualityNode qualityNode_g_a = new QualityNode();
        qualityNode_g_a.setName("接收").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_g_a = qualityTreeRepository.saveAndFlush(qualityNode_g_a);

        QualityNode qualityNode_g_b = new QualityNode();
        qualityNode_g_b.setName("历史").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_g_b = qualityTreeRepository.saveAndFlush(qualityNode_g_b);

        QualityNode qualityNode_h = new QualityNode();
        qualityNode_h.setName("指令回复单").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_h = qualityTreeRepository.saveAndFlush(qualityNode_h);

        QualityNode qualityNode_h_a = new QualityNode();
        qualityNode_h_a.setName("新增").setParentId(qualityNode_h.getId()).setRootId(root.getId());
        qualityNode_h_a = qualityTreeRepository.saveAndFlush(qualityNode_h_a);

        QualityNode qualityNode_h_b = new QualityNode();
        qualityNode_h_b.setName("审核").setParentId(qualityNode_h.getId()).setRootId(root.getId());
        qualityNode_h_b = qualityTreeRepository.saveAndFlush(qualityNode_h_b);

        QualityNode qualityNode_h_c = new QualityNode();
        qualityNode_h_c.setName("历史").setParentId(qualityNode_h.getId()).setRootId(root.getId());
        qualityNode_h_c = qualityTreeRepository.saveAndFlush(qualityNode_h_c);

        QualityNode qualityNode_i = new QualityNode();
        qualityNode_i.setName("质量检查及整改情况").setParentId(root.getId()).setRootId(root.getId());
        qualityNode_i = qualityTreeRepository.saveAndFlush(qualityNode_i);

        QualityNode qualityNode_i_a = new QualityNode();
        qualityNode_i_a.setName("整改情况").setParentId(qualityNode_i.getId()).setRootId(root.getId());
        qualityNode_i_a = qualityTreeRepository.saveAndFlush(qualityNode_i_a);

        QualityNode qualityNode_i_b = new QualityNode();
        qualityNode_i_b.setName("查看").setParentId(qualityNode_i.getId()).setRootId(root.getId());
        qualityNode_i_b = qualityTreeRepository.saveAndFlush(qualityNode_i_b);

        QualityNode qualityNode_i_a_a = new QualityNode();
        qualityNode_i_a_a.setName("新建").setParentId(qualityNode_i_a.getId()).setRootId(root.getId());
        qualityNode_i_a_a = qualityTreeRepository.saveAndFlush(qualityNode_i_a_a);

        QualityNode qualityNode_i_a_b = new QualityNode();
        qualityNode_i_a_b.setName("审核").setParentId(qualityNode_i_a.getId()).setRootId(root.getId());
        qualityNode_i_a_b = qualityTreeRepository.saveAndFlush(qualityNode_i_a_b);
        String jsonRes = null;
        try {
            jsonRes = selectAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonRes;
    }


    @Override
    public String selectAll() throws Exception {
        Gson gson = new Gson();
        //根节点
        Set<QualityNode> nodeSet = qualityTreeRepository.getByParentIdOrderByCreateTime(ROOT_PARENT_ID);
        if (nodeSet.size() == 1) {
            QualityNode qNode = new QualityNode();
            for (QualityNode n : nodeSet) {
                qNode = n;
            }
            JsonObject json = new JsonObject();
            JsonArray qNodeArray = new JsonArray();
            json.add("nodes", qNodeArray);
            setNodeJsonInfo(json, qNode);
            qNodeArray.add(setNodeArray(qNode));
            return json.toString();
        } else {
            System.out.println("存在多个根节点");
        }
        return null;
    }

    @Override
    public QualityNode selectById(final String id) {
        return qualityTreeRepository.getOne(id);
    }

    @Override
    public Set<QualityNode> selectByParentId(final String parentId) {
        return qualityTreeRepository.getByParentIdOrderByCreateTime(parentId);
    }

    @Override
    public QualityNode updateNode(final QualityNode qualityNode) {
        return qualityTreeRepository.saveAndFlush(qualityNode);
    }

    @Override
    public void deleteById(final String id) {
        qualityTreeRepository.delete(id);
    }

    private JsonObject setNodeArray(final QualityNode qNode) {
        JsonObject jsonNode = new JsonObject();
        setNodeJsonInfo(jsonNode, qNode);
        createTreeJson(jsonNode, qNode);
        return jsonNode;
    }

    public void createTreeJson(final JsonObject jsonObj, QualityNode parentNode) {
        JsonArray subNodeArray = new JsonArray();
        for (final QualityNode qNode : qualityTreeRepository.getByParentIdOrderByCreateTime(parentNode.getId())){
            JsonObject jsonObject = new JsonObject();
            setNodeJsonInfo(jsonObject, qNode);
            subNodeArray.add(jsonObject);
            jsonObj.add("nodes", subNodeArray);
            createTreeJson(jsonObject, qNode);
        }
    }

    private JsonObject setNodeJsonInfo(final JsonObject jsonObject, QualityNode qualityNode){
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_ID, qualityNode.getId());
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_NAME, qualityNode.getName());
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_PARENTID, qualityNode.getParentId());
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_ROOTID, qualityNode.getRootId());
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_DESCRIPTION, qualityNode.getDescription());
        jsonObject.addProperty(QUALITY_NODE_JSON_KEY_TYPE, qualityNode.getType());
        return jsonObject;
    }
}
